package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
//        //根据toUserId查询，根据评分score排序，获取第一条
//        //构建Criteria
       Criteria criteria =Criteria.where("toUserId").is(toUserId);
//        //构建Query对象
       Query query=new Query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(1);//查第一条
//        //调用mongoTemplate查询
      RecommendUser recommendUsers = mongoTemplate.findOne(query, RecommendUser.class);
      return recommendUsers;
    }

    @Override
    public PageResult queryRecommendUserList(Integer page, Integer pagesize, Long userId) {
        //1、构建Criteria对象
        Criteria criteria =Criteria.where("toUserId").is(userId);
        Query query =new Query(criteria);
        //查询总记录苏
        long counts = mongoTemplate.count(query, RecommendUser.class);
        int count = (int) counts;
        Query score = query.with(Sort.by(Sort.Order.desc("score"))).skip((page - 1) * pagesize).limit(pagesize);
        //3、调用mongoTemplate查询
        List<RecommendUser> list = mongoTemplate.find(score, RecommendUser.class);
        //4、利用PageResult方法构建返回值PageResult
        return new PageResult(page,pagesize,count,list);
    }

    //根据用户id和推荐用户id查推荐表
    public RecommendUser findRecommendUser(Long userId, Long id) {
        Criteria criteria=Criteria.where("userId").is(userId).and("toUserId").is(id);
        Query query=Query.query(criteria);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser==null){
            recommendUser=new RecommendUser();
            recommendUser.setUserId(userId);
            recommendUser.setToUserId(id);
            recommendUser.setScore(95d);
        }
        return recommendUser;
    }

    @Override
    public List<RecommendUser> queryCardsList(Long userId, int i) {
        //1、查询喜欢不喜欢的用户ID
        List<UserLike> likeList = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)), UserLike.class);
        List<Long> likeUserIdS = CollUtil.getFieldValues(likeList, "likeUserId", Long.class);
        //2、构造查询推荐用户的条件
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserIdS);
        //3、使用统计函数，随机获取推荐的用户列表
        TypedAggregation<RecommendUser> newAggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria),//指定查询条件 排除这些条件？
                Aggregation.sample(i)
        );
        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(newAggregation, RecommendUser.class);
        //4、构造返回
        return results.getMappedResults();
    }

    @Override//查推荐用户评分
    public List<RecommendUser> findRecommendUsers(List<Long> list,Long userId) {
        Query query =Query.query(Criteria.where("userId").in(list).and("toUserId").is(userId));
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        return recommendUserList;
    }
}
